﻿using MapleLeaf.Application.CookBook;
using MapleLeaf.Application.CookBook.Models;
using MapleLeaf.Core;
using MapleLeaf.Core.Dto;
using MapleLeaf.Model.CookBook.Organization.Entity;
using MapleLeaf.Service.CookBook.Models;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace MapleLeaf.WebApi.CookBook.Controllers.SystemManagement {
    /// <summary>
    /// 角色管理
    /// </summary>
    public class RolesController : ApiBaseController {

        private readonly SystemClient _systemClient;
        private readonly OrganizationClient _organizationClient;
        /// <summary>
        /// 构造函数
        /// </summary>
        public RolesController(OrganizationClient organizationClient, SystemClient systemClient) {
            _organizationClient = organizationClient;
            _systemClient = systemClient;
        }

        /// <summary>
        /// 获取角色分页列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PaginationModel<RoleVo>>> GetRolePageList(int pageNum, int pageSize) {
            var whereExp = Expressionable.Create<RoleEntity>();
            var pagemodel = new PageModel() {
                 PageIndex = pageNum,
                 PageSize = pageSize,
            };
            var list = await _organizationClient.GetRolePageListAsync(whereExp.ToExpression(), pagemodel);

            PaginationModel<RoleVo> result = new() {
                PageIndex = pageNum,
                PageSize = pageSize,
                Total = pagemodel.TotalCount,
                List = list.Adapt<List<RoleVo>>()
            };
            return Success(result);
        }
        /// <summary>
        /// 获取角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<RoleVo>> GetRole(long id) {
            var entity = await _organizationClient.GetRoleInfoAsync(id);
            var vo = entity.Adapt<RoleVo>();
            return Success(vo);
        }
        /// <summary>
        /// 获取角色-选项
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<OptionType>>> GetRoleOptions() { 
            var list = await _organizationClient.GetRoleOptionsAsync();
            return Success(list);
        }
        /// <summary>
        /// 保存角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> SaveRole(RoleVo menu) {
            var entity = menu.Adapt<RoleEntity>();
            _ = await _organizationClient.SaveRoleAsync(entity);
            return Success();
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> DeleteRole(long menuId) {
            var res = await _organizationClient.DeleteRoleAsync([menuId]);
            return res ? Success() : Fail();
        }


        
    }
}
